package com.zhihuishu.micro.course.openapi.app.impl.teacher;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zhihuishu.micro.course.model.WonderVideo;
import com.zhihuishu.micro.course.openapi.app.dto.WonderVideoOpenDto;
import com.zhihuishu.micro.course.openapi.app.teacher.WonderVideoOpenService;
import com.zhihuishu.micro.course.service.WonderVideoService;
import com.zhihuishu.remote.ErrorCode;
import com.zhihuishu.remote.RemoteException;
import com.zhihuishu.remote.RemoteResult;

/** 
 * @author yulijin 
 * @version 创建时间：2016年12月15日 下午5:10:19 
 * 类说明 
 */
@Service(value="wonderVideoOpenServiceForTeacherApp")
public class WonderVideoOpenServiceImpl implements WonderVideoOpenService {
    @Resource
    private WonderVideoService wonderVideoService;
	@Override
	public RemoteResult<WonderVideoOpenDto> save(
			WonderVideoOpenDto wonderVideoOpenDto) throws RemoteException {
		RemoteResult<WonderVideoOpenDto> result = new RemoteResult<WonderVideoOpenDto>();
        // 参数检查
        if (wonderVideoOpenDto.getCourseId() == null) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.ARGUMENTS_ILLEGALITY);
            return result;
        }
        try {
        	WonderVideo wonderVideo = wonderVideoService.save(transModel(wonderVideoOpenDto));
        	wonderVideoOpenDto = transDto(wonderVideo,wonderVideoOpenDto);
        	result.setResult(wonderVideoOpenDto);
        } catch (Exception e) {
        	e.printStackTrace();
            throw new RemoteException(e).setMessage("保存精彩视频出错!");
        }

        return result;
	}

	@Override
	public RemoteResult<Void> update(WonderVideoOpenDto wonderVideoOpenDto)
			throws RemoteException {
		 RemoteResult<Void> result = new RemoteResult<Void>();
        // 参数检查
        if (wonderVideoOpenDto.getWonderVideoId() == null) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.ARGUMENTS_ILLEGALITY);
            return result;
        }
        try {
        	wonderVideoService.update(transModel(wonderVideoOpenDto));
        } catch (Exception e) {
        	e.printStackTrace();
            throw new RemoteException(e).setMessage("更新精彩视频出错!");
        }
	    return result;
	}

	@Override
	public RemoteResult<Void> remove(Long wonderVideoId) throws RemoteException {
		 RemoteResult<Void> result = new RemoteResult<Void>();
	        // 参数检查
        if (wonderVideoId == null) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.ARGUMENTS_ILLEGALITY);
            return result;
        }
        try {
        	wonderVideoService.remove(wonderVideoId);;
        } catch (Exception e) {
        	e.printStackTrace();
            throw new RemoteException(e).setMessage("删除精彩视频出错!");
        }
	    return result;
	}

	@Override
	public RemoteResult<WonderVideoOpenDto> findWonderVideo(Long wonderVideoId)
			throws RemoteException {
		RemoteResult<WonderVideoOpenDto> result = new RemoteResult<WonderVideoOpenDto>();
        // 参数检查
        if (wonderVideoId == null) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.ARGUMENTS_ILLEGALITY);
            return result;
        }
        try {
        	WonderVideo wonderVideo = wonderVideoService.findWonderVideo(wonderVideoId);
        	WonderVideoOpenDto dto = null ;
        	if(wonderVideo != null){
        		dto = new WonderVideoOpenDto();
        		dto = transDto(wonderVideo,dto);
        	}
        	result.setResult(dto);
        } catch (Exception e) {
        	e.printStackTrace();
            throw new RemoteException(e).setMessage("查询精彩视频出错!");
        }
        
        return result;
	}

	@Override
	public RemoteResult<List<WonderVideoOpenDto>> findWonderVideosByCourseId(
			Long courseId) throws RemoteException {
		RemoteResult<List<WonderVideoOpenDto>> result = new RemoteResult<List<WonderVideoOpenDto>>();
        // 参数检查
        if (courseId == null) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.ARGUMENTS_ILLEGALITY);
            return result;
        }
        try {
        	List<WonderVideo> wonderVideos = wonderVideoService.findWonderVideosByCourseId(courseId);
        	List<WonderVideoOpenDto> dtos = null;
        	if(wonderVideos !=null && wonderVideos.size()>0){
        		dtos = new ArrayList<WonderVideoOpenDto>();
        		WonderVideoOpenDto dto = null;
        		for(WonderVideo model : wonderVideos){
        			dto = new WonderVideoOpenDto();
        			dto = transDto(model,dto);
        			if(dto != null){
        				dtos.add(dto);
        			}
        		}
        	}
        	result.setResult(dtos);
        } catch (Exception e) {
        	e.printStackTrace();
            throw new RemoteException(e).setMessage("查询精彩视频出错!");
        }
        
        return result;
	}
	
	//手动转换比工具包效率高
	private WonderVideo transModel(WonderVideoOpenDto dto){
		WonderVideo model = null;
		if(dto != null){
			model = new WonderVideo();
			model.setWonderVideoId(dto.getWonderVideoId());
			model.setImg(dto.getImg());
			model.setVideoId(dto.getVideoId());
			model.setCourseId(dto.getCourseId());
			model.setCreateTime(dto.getCreateTime());
			model.setUpdateTime(dto.getUpdateTime());
			model.setIsDelete(dto.getIsDelete());
		}
		return model;
	}
	
	private WonderVideoOpenDto transDto(WonderVideo model,WonderVideoOpenDto dto){
		if(model != null){
			dto.setWonderVideoId(model.getWonderVideoId());
			dto.setImg(model.getImg());
			dto.setVideoId(model.getVideoId());
			dto.setCourseId(model.getCourseId());
			dto.setCreateTime(model.getCreateTime());
			dto.setUpdateTime(model.getUpdateTime());
			dto.setIsDelete(model.getIsDelete());
		}
		return dto;
	}
}
